import pandas as pd
from sqlalchemy import create_engine
from utils.config import get_config

def product_details_database():
    """
    将数据写入数据库
    """
    # 读取CSV文件
    df = pd.read_csv(f'../../data_csv/product_id/jd_product_details.csv', header=0)
    df['product_id'] = df['product_id'].astype(str)


    # 添加新的列'rank'，值从1开始递增
    df = df.assign(rank=lambda x: range(1, len(x) + 1))

    config = get_config('../../server_config.ini')

    # 连接到PostgreSQL数据库
    engine = create_engine('postgresql://' + config['user'] + ':' + config['password'] + '@' +
                           config['host'] + ':' + config['port'] + '/' + config['database'])

    # 将数据写入数据库
    df.to_sql('phone_params', engine, if_exists='append', index=False)